package com.example.aliceapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;

public class ButtonView extends Activity {

	private Button topLeftButton;
	private Button upButton;
	private Button topRightButton;

	private Button rightButton;
	private Button NOSButton;
	private Button leftButton;

	private Button bottomLeftButton;
	private Button downButton;
	private Button bottomRightButton;

	private Button accelerometer;
	private Button startRecording;
	private Button circleButton;

	private boolean isRecording = false;

	private BluetoothConnector bConnector;

	private ArrayList<Button> buttonList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity_controller);
		initButtons();
		setButtonListeners();
		bConnector = BluetoothConnector.getInstance();

		GridView buttonPad = (GridView) findViewById(R.id.buttonPad);
		buttonPad.setAdapter(new GridViewAdapter(buttonList));
	}
	
	private void initButtons() {

		buttonList = new ArrayList<Button>();

		// First Row
		topLeftButton = new Button(this);
		topLeftButton.setText(getResources().getString(R.string.TopLeft));
		buttonList.add(topLeftButton);
		upButton = new Button(this);
		upButton.setText(getResources().getString(R.string.Up));
		buttonList.add(upButton);
		topRightButton = new Button(this);
		topRightButton.setText(getResources().getString(R.string.TopRight));
		buttonList.add(topRightButton);

		// Second Row
		leftButton = new Button(this);
		leftButton.setText(getResources().getString(R.string.Left));
		buttonList.add(leftButton);
		NOSButton = new Button(this);
		NOSButton.setText(getResources().getString(R.string.NOS));
		buttonList.add(NOSButton);
		rightButton = new Button(this);
		rightButton.setText(getResources().getString(R.string.Right));
		buttonList.add(rightButton);

		// Third Row
		bottomLeftButton = new Button(this);
		bottomLeftButton.setText(getResources().getString(R.string.BottomLeft));
		buttonList.add(bottomLeftButton);
		downButton = new Button(this);
		downButton.setText(getResources().getString(R.string.Down));
		buttonList.add(downButton);
		bottomRightButton = new Button(this);
		bottomRightButton.setText(getResources()
				.getString(R.string.BottomRight));
		buttonList.add(bottomRightButton);

		// Accelerometer button
		accelerometer = new Button(this);
		accelerometer.setText(getResources().getString(R.string.Accelerometer));
		buttonList.add(accelerometer);
		// accelerometer.setEnabled(false);

		// Recording button
		startRecording = new Button(this);
		startRecording.setText(getResources().getString(R.string.Recording));
		buttonList.add(startRecording);

		circleButton = new Button(this);
		circleButton.setText(getResources().getString(R.string.Circle));
		buttonList.add(circleButton);

	}

	private void setButtonListeners() {
		upButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveForward();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		topLeftButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveNorthWest();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		topRightButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveNorthEast();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		rightButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveRight();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		circleButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		leftButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveLeft();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		bottomLeftButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveSouthWest();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		downButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveBackward();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		bottomRightButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.moveSouthEast();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

		accelerometer.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {

					Intent controlIntent = new Intent(ButtonView.this,
							Accelerometer.class);
					startActivity(controlIntent);

				}
				return false;
			}
		});

		startRecording.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (isRecording) {
					startRecording.setText(R.string.Recording);
					bConnector.goHome();
				} else {
					bConnector.clearStack();
					startRecording.setText(R.string.Play);
				}
				isRecording = !isRecording;
			}
		});

		NOSButton.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					bConnector.NOS();
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					bConnector.stop();
				}
				return false;
			}
		});

	}

	private class GridViewAdapter implements ListAdapter {

		private ArrayList<Button> buttonList;

		public GridViewAdapter(ArrayList<Button> buttonList) {
			this.buttonList = buttonList;
		}

		@Override
		public int getCount() {
			return buttonList.size();
		}

		@Override
		public Object getItem(int index) {
			return buttonList.get(index);
		}

		@Override
		public long getItemId(int index) {
			return buttonList.get(index).getId();
		}

		@Override
		public int getItemViewType(int index) {
			return 0;
		}

		@Override
		public View getView(int pos, View convertView, ViewGroup parent) {

			return buttonList.get(pos);
		}

		@Override
		public int getViewTypeCount() {
			return 1;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isEmpty() {
			// TODO Auto-generated method stub
			return buttonList.isEmpty();
		}

		@Override
		public void registerDataSetObserver(DataSetObserver arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean areAllItemsEnabled() {
			return true;
		}

		@Override
		public boolean isEnabled(int position) {
			return true;
		}

	}
}
